package cn.hxth.cloud.consumer.service.impl;


import cn.hxth.cloud.api.domain.Member;
import cn.hxth.cloud.consumer.mapper.ActionMapper;
import cn.hxth.cloud.consumer.mapper.MemberMapper;
import cn.hxth.cloud.consumer.mapper.RoleMapper;
import cn.hxth.cloud.consumer.service.IMemberService;
import cn.hxth.cloud.consumer.util.PasswordUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Service
public class MemberServiceImpl implements IMemberService {
	@Resource
	private MemberMapper memberMapper ;
	@Resource
	private RoleMapper roleMapper ;
	@Resource
	private ActionMapper actionMapper ;


	public Member get(String mid) {
		return this.memberMapper.findById(mid);
	}

	@Override
	public Map<String, Set<String>> getRoleAndActionByMember(String mid) {
		Map<String, Set<String>> map = new HashMap<>() ;
		map.put("allRoles", this.roleMapper.findAllByMember(mid)) ;
		map.put("allActions", this.actionMapper.findAllByMember(mid)) ;
		return map;
	}

	@Override
	public void initAdmin() {
		int count = this.memberMapper.selectCount() ;
		if(count==0){
			Member member = new Member() ;
			member.setPassword(PasswordUtil.encoder("hello"));
			member.setName("管理员");
			member.setMid("admin");
			this.memberMapper.doCreate(member) ;
		}
	}
}

